﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.Models;
namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class HopitalManagerController : Controller
    {
        //
        // GET: /HopitalManager/

        VNInsuranceDataContext db = new VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult listhop()
        {
            var benlang = db.HopitalLangs.Where(m => m.LangID == Request.Cookies["_culture"]["lang"]).Select(m => new
            {
                HopitalID = m.HopitalID,
                LangID = m.LangID,
                HopitalName = m.HopitalName,
                Adress = m.Address,
                Phone = m.Phone,
                Fax = m.Fax,
                Website=m.Website
            });
            return Json(benlang, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult AddHopital()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddHopital(HopitalLang hplang)
        {
            Hopital hop = new Hopital();
            
            HopitalLang hoplang = new HopitalLang();
            HopitalLang hoplang1 = new HopitalLang();
            var isexit = db.Hopitals.Where(m => m.HopitalName == hplang.HopitalName).FirstOrDefault();
            if (isexit == null)
            {
                hop.HopitalName = hplang.HopitalName;
                db.Hopitals.InsertOnSubmit(hop);
                db.SubmitChanges();
                hoplang.HopitalID = hop.ID;
                hoplang.HopitalName = hop.HopitalName;
                hoplang.LangID = Request.Cookies["_culture"]["lang"];
                hoplang.Address = hplang.Address;
                hoplang.Phone = hplang.Phone;
                hoplang.Fax = hplang.Fax;
                hoplang.Website = hplang.Website;
                db.HopitalLangs.InsertOnSubmit(hoplang);
                if (hoplang.LangID == "en-US")
                {
                    hoplang1.LangID = "vi-VN";
                }
                else
                {
                    hoplang1.LangID = "en-US";
                }
                hoplang1.HopitalID = hop.ID;
                hoplang1.HopitalName = hop.HopitalName;
                hoplang1.Address = hplang.Address;
                db.HopitalLangs.InsertOnSubmit(hoplang1);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return Json("Garaname is exist");
            }
        }
        [HttpGet]
        public ActionResult EditHopital(int id)
        {
            var hopital = db.HopitalLangs.Where(m => m.HopitalID == id && m.LangID == Request.Cookies["_culture"]["lang"]).FirstOrDefault();
            return View(hopital);
        }
        [HttpPost]
        public ActionResult EditHopital(HopitalLang hopitlang)
        {
            var hopfound = db.HopitalLangs.Where(m => m.HopitalID == hopitlang.HopitalID && m.LangID == hopitlang.LangID).FirstOrDefault();
            if (hopfound != null)
            {
                hopfound.HopitalName = hopitlang.HopitalName;
                hopfound.Address = hopitlang.Address;
                hopfound.Phone = hopitlang.Phone;
                hopfound.Fax = hopitlang.Fax;
                hopfound.Website = hopitlang.Website;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
            
        }
        [HttpPost]
        public ActionResult RemoveHopital(int id)
        {
            var hopfound = db.HopitalLangs.Where(m => m.HopitalID == id);
            db.HopitalLangs.DeleteAllOnSubmit(hopfound);
            db.SubmitChanges();
            var hop = db.Hopitals.Where(m => m.ID ==id).SingleOrDefault();
            db.Hopitals.DeleteOnSubmit(hop);
            db.SubmitChanges();
            return Json(true);
        }
    }
}
